﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace DataAchive.Helper
{
    public class ConnectionHelper
    {
        public static Dictionary<string,string> siteAndConnections = new Dictionary<string, string>();

        public static Dictionary<string,List<string>>siteAndMachineids = new Dictionary<string, List<string>>();

        public static string CurrentSiteAddress { get; set; }
       
        public static void GetXmlSetting()
        {
            try
            {
                if (siteAndConnections.Count() == 0)
                {
                    XmlNode dbNodes = Setting.GetXmlNode("setting/MySqlDB");
                    if (dbNodes != null)
                    {
                        for (int i = 0; i < dbNodes.ChildNodes.Count; i++)
                        {
                            if (dbNodes.ChildNodes[i].NodeType == XmlNodeType.Element)
                            {
                                var childNodes = dbNodes.ChildNodes[i].ChildNodes;
                                if (childNodes != null)
                                {
                                    for (int j = 0; j < childNodes.Count; j++)
                                    {
                                        var constr = childNodes[0].InnerText;
                                        var sitename = childNodes[1].InnerText;
                                        if (!siteAndConnections.ContainsKey(sitename))
                                        {
                                            siteAndConnections.Add(sitename, constr);
                                        }

                                    }
                                }

                            }

                        }

                    }

                }
            }
            catch (Exception e)
            {

                SoftCommon.Log.Write(e);
            }
          


        }


        public static void GetXmlMachineSetting()
        {
            try
            {
                if (siteAndMachineids.Count() == 0)
                {
                    XmlNode dbNodes = Setting.GetXmlNode("setting/MachineType");
                    if (dbNodes != null)
                    {
                        for (int i = 0; i < dbNodes.ChildNodes.Count; i++)
                        {
                            if (dbNodes.ChildNodes[i].NodeType == XmlNodeType.Element)
                            {
                                var typeName = dbNodes.ChildNodes[i].Attributes["Name"]?.Value;
                                if (!string.IsNullOrWhiteSpace(typeName) && !siteAndMachineids.ContainsKey(typeName))
                                {
                                    var childNodes = dbNodes.ChildNodes[i].ChildNodes;
                                    List<string> machineList = new List<string>();

                                    if (childNodes != null)
                                    {

                                        for (int j = 0; j < childNodes.Count; j++)
                                        {
                                            var machineN = childNodes[j].InnerText;
                                            machineList.Add(machineN);

                                        }

                                    }
                                    siteAndMachineids.Add(typeName, machineList);
                                }


                            }

                        }

                    }

                }
            }
            catch (Exception e)
            {

                SoftCommon.Log.Write(e);
            }
          


        }

    }
}
